package org.restorator.dao.implement;

import java.util.ArrayList;
import java.util.List;

import org.restorator.dao.base.UserDao;
import org.restorator.entity.Order;
import org.restorator.entity.User;
import org.restorator.exception.DataStoreException;

public class UserDaoMock implements UserDao {
	
	private List<User> list = new ArrayList<User>();
	
	public UserDaoMock() {
		User user = new User();
		user.setUserId(1);
		user.setLogin("waiter");
		user.setPassword("12345");
		user.setName("Mary");
		user.setSurname("Smith");
		user.setPhone("435657");	
		user.setRoleId(1);
		list.add(user);
		
	}

	@Override
	public User get(Integer id) throws DataStoreException {
		for(User u: list) {
			if(u.getUserId()==id) {
				return u;
			}
		}
		return null;
	}

	@Override
	public User update(User user) throws DataStoreException {
		for (User u: list) {
			if(u.getUserId()==user.getUserId()) {
				u.setUserId(user.getUserId());
				u.setLogin(user.getLogin());
				u.setPassword(user.getPassword());
				u.setName(user.getName());
				u.setSurname(user.getSurname());
				u.setPhone(user.getPhone());
				u.setRoleId(user.getRoleId());
			}
		}
		return user;
	}

	@Override
	public void delete(User user) {
		for (User u: list) {
			if(u.getUserId()==user.getUserId()) {
				list.remove(u);
			}
		}
		
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Integer save(User user, Object... addParams)
			throws DataStoreException {
		list.add(user);
		return user.getUserId();
	}

	@Override
	public List<User> getAll() throws DataStoreException {
		return list;
	}

	@Override
	public boolean exist(Integer id) {
		for (User u: list) {
			if(u.getUserId()==id) {
				return true;
			}
		}
		return false;
	}

	@Override
	public User getUserByOrderId(int orderId) throws DataStoreException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User getUserByLoginAndPass(String password, String login) {
        System.out.println("pass " + password + " login " + login);
        System.out.println("Exist: " + list);
        for (User u: list) {
			if((u.getLogin().equals(login)) && (u.getPassword().equals(password))) {
				return u;
			}
		}
		return null;
	}

    @Override
    public User getUserByKey(String key) throws DataStoreException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
